//
//  ChatTaBarView_controller.swift
//  WeiXin1.0
//
//  Created by xyy on 2017/11/6.
//  Copyright © 2017年 xyy. All rights reserved.
//

import UIKit

class MyTaBarView_controller: UITabBarController {

    var tabbarBack: UIView!
    var lastBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //执行加载控制器
        self.loadViewControllers();
        //自定义底部tabBar
        self.tabbarBack = UIView(frame: self.tabBar.frame)
        self.tabbarBack.backgroundColor = UIColor.white
        self.tabbarBack.backgroundColor?.withAlphaComponent(0.9)
        //添加并替换
        self.view.addSubview(self.tabbarBack)
        self.tabBar.isHidden = true
        //初始化底部tabBar
        self.initTabBar()
    }
    
    func loadViewControllers()  {
        //第一页 聊天
//        let chatVC: ContactsTableViewController = ContactsTableViewController()
//        let chatNC = UINavigationController(rootViewController: chatVC)
        let chatVC: ChatTableViewController = ChatTableViewController()
        let chatNC = UINavigationController(rootViewController: chatVC)
        //第二页 通讯录
//        let contactsVC: ExploreTableViewController = ExploreTableViewController()
//        let contactsNC = UINavigationController(rootViewController: contactsVC)
        let contactsVC: ContactsTableViewController = ContactsTableViewController()
        let contactsNC = UINavigationController(rootViewController: contactsVC)
        //第三页 发现
        let exploreVC: ExploreTableViewController = ExploreTableViewController()
        let exploreNC = UINavigationController(rootViewController: exploreVC)
        //第四页 我
        let meVC: MeTableViewController = MeTableViewController()
        let meNC = UINavigationController(rootViewController: meVC)
        
        // 5.新建一个导航控制器实例的数组
        let controllersArray: NSArray = [chatNC, contactsNC,exploreNC,meNC]
        // 6.把导航控制器的数组加入到标签控制器
        self.setViewControllers(controllersArray as? [UIViewController], animated: true)
        for i in 0..<controllersArray.count {
            // 设置导航栏背景颜色
            (controllersArray[i] as! UINavigationController).navigationBar.backgroundColor = UIColor.black
            // 设置导航栏样式
            (controllersArray[i] as! UINavigationController).navigationBar.barStyle = .blackTranslucent
            // 设置返回按钮文字和图标颜色
            (controllersArray[i] as! UINavigationController).navigationBar.tintColor = UIColor.white
        }
    }
    
    func initTabBar()  {
        let btnWidth: Int = Int(self.tabBar.frame.size.width)/self.viewControllers!.count;
        for i in 0..<self.viewControllers!.count {
            // 创建自定义按钮
            let btn: UIButton = UIButton(type: .custom)
            // 按钮坐标和尺寸
            let rect: CGRect = CGRect(x: btnWidth*i, y: 4, width: btnWidth, height: 40)
            btn.frame = rect
            // 设置普通状态图片
            btn.setImage(UIImage(named: "tabbar\(i+1)"), for: UIControlState.normal)
            // 设置选中状态图片
            btn.setImage(UIImage(named: "tabbarsel\(i+1)"), for: UIControlState.selected)
            // 设置普通状态标题
            switch i {
            case 0:
                btn.setTitle("微信", for: UIControlState.normal)
            case 1:
                btn.setTitle("通讯录", for: UIControlState.normal)
            case 2:
                btn.setTitle("发现", for: UIControlState.normal)
            case 3:
                btn.setTitle("我", for: UIControlState.normal)
            default:
                print("error")
            }
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 10)
            // 设置按钮普通状态标题颜色
            btn.setTitleColor(UIColor.init(red: 122.0/255.0, green: 126.0/255.0, blue: 131.0/255.0, alpha: 1.0), for: UIControlState.normal)
            // 设置按钮高亮或选中状态标题颜色
            btn.setTitleColor(UIColor.init(red: 26.0/255.0, green: 173.0/255.0, blue: 25.0/255.0, alpha: 1.0), for: UIControlState.selected)
            btn.addTarget(self, action: #selector(btnClick(_:)), for: UIControlEvents.touchUpInside)
            btn.tag = i
            
            // 按钮图片和标题总高度
            let totalHeight: CGFloat = (btn.imageView!.frame.size.height + btn.titleLabel!.frame.size.height);
            // 设置按钮图片偏移
            btn.imageEdgeInsets = UIEdgeInsets(top: -(totalHeight - (btn.imageView?.frame.size.height)!), left: 0, bottom: 0, right: -(btn.titleLabel?.frame.size.width)!)
            
            // 设置按钮标题偏移
            btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(btn.imageView?.frame.size.width)!, bottom: -(totalHeight - (btn.titleLabel?.frame.size.height)!)-8, right: 0)
            self.tabbarBack.addSubview(btn)
            if i==0 {
                btn.isSelected = true
                self.lastBtn = btn
            }
        }
    }
    @objc func btnClick(_ btn: UIButton)  {
        self.lastBtn.isSelected = false
        btn.isSelected = true
        //选中按钮切换到对应的控制器上
        self.selectedIndex = btn.tag
        self.lastBtn = btn;
    }
}
